package com.rx.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 系统配置
 * 
 * @author Renxin
 * 
 */
public class Config {

	protected static Logger logger = LoggerFactory.getLogger(Config.class);

	/* 配置映射 */
	private static Map<String, Object> configureMapping = new HashMap<String, Object>();

	/**
	 * 获取String
	 * 
	 * @param key
	 * @return
	 */
	public static String getString(String key) {
		return getString(key, null);
	}

	/**
	 * 获取String
	 * 
	 * @param key
	 * @return
	 */
	public static String getString(String key, String defaultVal) {
		try {
			return (String) getConfigureMapping().get(key);
		} catch (Exception e) {
			logger.error("获取String类型的系统配置出错，错误key->{}", key);
		}
		return defaultVal;
	}

	/**
	 * 获取Integer
	 * 
	 * @param key
	 * @return
	 */
	public static Integer getInteger(String key) {
		return getInteger(key, null);
	}

	/**
	 * 获取Integer
	 * 
	 * @param key
	 * @return
	 */
	public static Integer getInteger(String key, Integer defaultVal) {
		try {
			return (Integer) getConfigureMapping().get(key);
		} catch (Exception e) {
			logger.error("获取Integer类型的系统配置出错，错误key->{}", key);
		}
		return defaultVal;
	}

	/**
	 * 获取Long
	 * 
	 * @param key
	 * @return
	 */
	public static Long getLong(String key) {
		return getLong(key, null);
	}

	/**
	 * 获取Long
	 * 
	 * @param key
	 * @return
	 */
	public static Long getLong(String key, Long defaultVal) {
		try {
			return (Long) getConfigureMapping().get(key);
		} catch (Exception e) {
			logger.error("获取Long类型的系统配置出错，错误key->{}", key);
		}
		return defaultVal;
	}

	/**
	 * 获取Float
	 * 
	 * @param key
	 * @return
	 */
	public static Float getFloat(String key) {
		return getFloat(key, null);
	}

	/**
	 * 获取Float
	 * 
	 * @param key
	 * @return
	 */
	public static Float getFloat(String key, Float defaultVal) {
		try {
			return (Float) getConfigureMapping().get(key);
		} catch (Exception e) {
			logger.error("获取Float类型的系统配置出错，错误key->{}", key);
		}
		return defaultVal;
	}

	/**
	 * 获取Double
	 * 
	 * @param key
	 * @return
	 */
	public static Double getDouble(String key) {
		return getDouble(key, null);
	}

	/**
	 * 获取Double
	 * 
	 * @param key
	 * @return
	 */
	public static Double getDouble(String key, Double defaultVal) {
		try {
			return (Double) getConfigureMapping().get(key);
		} catch (Exception e) {
			logger.error("获取Double类型的系统配置出错，错误key->{}", key);
		}
		return defaultVal;
	}

	/**
	 * 获取List
	 * 
	 * @param key
	 * @return
	 */
	public static List<Object> getList(String key) {
		return getList(key, null);
	}

	/**
	 * 获取List
	 * 
	 * @param key
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Object> getList(String key, List<Object> defaultVal) {
		try {
			return (List<Object>) getConfigureMapping().get(key);
		} catch (Exception e) {
			logger.error("获取List类型的系统配置出错，错误key->{}", key);
		}
		return defaultVal;
	}

	/**
	 * 获取Map
	 * 
	 * @param key
	 * @return
	 */
	public static Map<String, Object> getMap(String key) {
		return getMap(key, null);
	}

	/**
	 * 获取Map
	 * 
	 * @param key
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> getMap(String key,
			Map<String, Object> defaultVal) {
		try {
			return (Map<String, Object>) getConfigureMapping().get(key);
		} catch (Exception e) {
			logger.error("获取Map类型的系统配置出错，错误key->{}", key);
		}
		return defaultVal;
	}

	private static Map<String, Object> getConfigureMapping() {
		return configureMapping;
	}

	public void setConfigureMapping(Map<String, Object> configureMapping) {
		Config.configureMapping = configureMapping;
	}

}
